#include "ShipView.h"

using namespace view;

ShipView::ShipView(Entity* entity, SDLSurface* parentSurface, string accelerateImagePath) : EntityView(entity, parentSurface)
{
	accelerated = new SDLSurface();
	accelerated->loadFromImage(accelerateImagePath.c_str());
	temp = surface;
	initializeLifeMeter();
	initializeShieldMeter();
	superShield.reset(new SuperShieldView(((Ship*)entity)->getSuperShield(), parentSurface, getWidth(), getHeight()));
	Shield.reset(new SuperShieldView(((Ship*)entity)->getShield(), parentSurface, getWidth(), getHeight()));
}

ShipView::ShipView(Entity* entity, SDLSurface* parentSurface, int width, int height, string accelerateImagePath) : EntityView(entity, parentSurface, width, height)
{
	accelerated = new SDLSurface();
	accelerated->loadFromImage(accelerateImagePath.c_str());
	temp = surface;
	initializeLifeMeter();
	initializeShieldMeter();
	superShield.reset(new SuperShieldView(((Ship*)entity)->getSuperShield(), parentSurface, getWidth(), getHeight()));
	Shield.reset(new SuperShieldView(((Ship*)entity)->getShield(), parentSurface, getWidth(), getHeight()));
}

ShipView::~ShipView(void)
{
	delete accelerated;
	surface = temp;
}

void ShipView::setAcceleratingImage(string imagePath)
{
	this->acceleratingImage = imagePath;
}

string ShipView::getAcceleratingImage()
{
	return this->acceleratingImage;
}

void ShipView::paint(void)
{
	if (entity && ((!entity->isInvisibilityEnabled()) || (entity->isInvisibilityEnabled() && entity->isOwnEntity()))  && !entity->isDeadEnabled())
	{
		if (((Ship*)entity)->isAcelerated() && accelerated)
		{
			surface = accelerated;
			surface->rotate(entity->getRotation(), 1.0, 1); // rota para dejarla en la misma direccion que la anterior
		}
		else
		{
			surface = temp; // vuelve a poner la imagen original
			surface->rotate(entity->getRotation(), 1.0, 1); // rota para dejarla en la misma direccion que la anterior
		}

		updateMeters();
		drawLifeMeter();

		if (((Ship*)entity)->getShield() && ((Ship*)entity)->getShield()->isEnabled())
			drawShieldMeter();

		superShield->draw();
		Shield->draw();
		EntityView::paint(); // llama al metodo de la clase padre que se encarga de pintar
	}
}

void ShipView::initializeLifeMeter(void)
{
	int maximumProgress = ((Ship*)entity)->getLife();
	ShipLifeMonitor* lifeMonitor = new ShipLifeMonitor(0, maximumProgress, maximumProgress);
	lifeProperty.reset(new ShipLifeProperty((Ship*)entity));
	lifeProperty->addPropertyChangeListener(lifeMonitor);
	lifeMeter.reset(new SDLProgressBar(lifeMonitor, parentSurface));
	lifeMeter->setWidth(temp->getWidth());
	lifeMeter->setHeight(8);
	lifeMeter->setBorderColor(new Color(0, 255, 0, 255));
	lifeMeter->setBackgroundColor(new Color(0, 0, 0, 255));
	lifeMeter->setForegroundColor(new Color(0, 255, 0, 255));
}

void ShipView::initializeShieldMeter(void)
{
	int maximumProgress = SHIELD_MAX_LIFE; //((Ship*)entity)->getShieldEnergy();
	ShipShieldMonitor* shieldMonitor = new ShipShieldMonitor(0, maximumProgress, maximumProgress);
	shieldProperty.reset(new ShipShieldProperty((Ship*)entity));
	shieldProperty->addPropertyChangeListener(shieldMonitor);
	shieldMeter.reset(new SDLProgressBar(shieldMonitor, parentSurface));
	shieldMeter->setWidth(temp->getWidth());
	shieldMeter->setHeight(8);
	shieldMeter->setBorderColor(new Color(255, 255, 0, 255));
	shieldMeter->setBackgroundColor(new Color(0, 0, 0, 255));
	shieldMeter->setForegroundColor(new Color(255, 255, 0, 255));
}

void ShipView::drawLifeMeter(void)
{
	lifeMeter->setX((int)(getX()+0.5));
	lifeMeter->setY((int)(getY()+temp->getHeight()+5+0.5));
	lifeMeter->draw();
}

void ShipView::drawShieldMeter(void)
{
	shieldMeter->setX((int)(getX()+0.5));
	shieldMeter->setY((int)(getY()+temp->getHeight()+15+0.5));
	shieldMeter->draw();
}

void ShipView::updateMeters(void)
{
	lifeProperty.get()->notifyListener();
	shieldProperty.get()->notifyListener();
}
